package com.lenovo.agricultureclient.db;

import java.util.Date;

import com.lenovo.agricultureclient.bean.SensorValue;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
/**
 * 传感器值的数据库类
 * @author zongbingwu
 *
 */
public class SensorValueDB extends BaseDB 
{
	//表名称
	public static final String TABLE_NAME = "SensorValue";
	
	//表主键
	private static final String KEY_ROWID	= "_id";
	
	private static final String KEY_PM25       			= "pm25";
	private static final String KEY_CO2     			= "co2"; 
	
	private static final String KEY_LIGHT		     	= "light";
	private static final String KEY_AIR_T		    	= "airTemper";
	private static final String KEY_AIR_H		  		= "airHumid";
	private static final String KEY_SOIL_T   			= "soilTemper"; 
	private static final String KEY_SOIL_H   			= "soilHumid"; 
	
	private static final String KEY_UPDATA_TIME   		= "updataTime"; 
	
	//创建表的sql语句
	public static final String TABLE_CREATE ="CREATE TABLE IF NOT EXISTS " + TABLE_NAME+ 
	        " (" +
	        KEY_ROWID + " integer primary key autoincrement, " + 
	        KEY_PM25 + " int, " + 
	        KEY_CO2 + " int, " + 
	        KEY_LIGHT + " int, " + 
	        KEY_AIR_T + " int, " + 
	        KEY_AIR_H + " int, " + 
	        KEY_SOIL_T + " int, " + 
	        KEY_SOIL_H + " int, " + 
	        KEY_UPDATA_TIME + " long " +
			");" ;
	
	public SensorValueDB(Context ctx) 
	{
		super(ctx, true);
	}

	//获取表名称
	@Override
	protected String getTableName() 
	{
		return TABLE_NAME;
	}

	//将内存中的感器值对象转成键值对
	@Override
	public ContentValues getContentValuesByData(Object data) 
	{
		SensorValue sensor = (SensorValue)data;
		if(sensor == null)
		{
			return null;
		}
		ContentValues initialValues = new ContentValues();
		
		initialValues.put(KEY_PM25, sensor.getPm25());   
		initialValues.put(KEY_CO2, sensor.getCo2() );
		
        initialValues.put(KEY_LIGHT, sensor.getLight() );       
        initialValues.put(KEY_AIR_T, sensor.getAirTemper() );   
        initialValues.put(KEY_AIR_H, sensor.getAirHumid() );
        initialValues.put(KEY_SOIL_T, sensor.getSoilTemper() );  
        initialValues.put(KEY_SOIL_H, sensor.getAirHumid() );
        
        //更新时间以时间戳的方式保存
        if(sensor.getUpdataTime()==null){
        	sensor.setUpdataTime(new Date());
        }
        initialValues.put(KEY_UPDATA_TIME, sensor.getUpdataTime().getTime() ); 
        
        return initialValues;
	}

	//从cursor游标中解析出传感器值对象
	@Override
	public Object getDataByCursor(Cursor cursor) 
	{
		if(cursor==null)
		{
			return null;
		}
		SensorValue data = new SensorValue();
		data.setPm25(cursor.getInt(cursor.getColumnIndex(KEY_PM25)));
		data.setCo2(cursor.getInt(cursor.getColumnIndex(KEY_CO2)));
		
		data.setLight(cursor.getInt(cursor.getColumnIndex(KEY_LIGHT)));
		data.setAirTemper(cursor.getInt(cursor.getColumnIndex(KEY_AIR_T)));
		data.setAirHumid(cursor.getInt(cursor.getColumnIndex(KEY_AIR_H)));
		data.setSoilTemper(cursor.getInt(cursor.getColumnIndex(KEY_SOIL_T)));
		data.setSoilHumid(cursor.getInt(cursor.getColumnIndex(KEY_SOIL_H)));
		
		long millisec = cursor.getLong(cursor.getColumnIndex(KEY_UPDATA_TIME));
		if(millisec > 0){
			data.setUpdataTime(new Date(millisec));
		} else {
			data.setUpdataTime(new Date());
		}
		
		return data;
	}
	
	/**
	 * 从数据库中读取传感器的值，更新时间戳必须大于指定值
	 * @param timeMills	时间戳限制条件
	 * @param listener	读取结果的回调函数
	 */
	public void loadSensorByTime(long timeMills, OnDatabaseHandlerListener listener)
	{
		if(timeMills <=0)
		{
			loadDataFromDB(null, listener);
		}
		else 
		{
			loadDataFromDB(KEY_UPDATA_TIME+">="+timeMills, listener);
		}
	}
	
	//从数据库中删除时间戳小于指定值的传感器数据
	public void deleteSensorByTime(final long timeMills)
	{
		new Thread()
		{
			@Override
			public void run() 
			{
				super.run();
				synchronized (_writeLock)
		        {
			        mDBHelper.getWritableDatabase().delete(getTableName(), KEY_UPDATA_TIME+"<="+timeMills, null);
		        }
			}
		}.start();
	}
}
